require 'rubygame'

Rubygame.init()

def test_music() 
	mix = Rubygame::Mixer
	# Use the lines below to get rid of artsd and contact ALSA directly on Linux.
	# ARTSD happens to be buggy on my old, old linux distro. 
	if RUBY_PLATFORM =~ /linux/
		`killall artsd`
		ENV['SDL_AUDIODRIVER'] = "alsa"
	end	
	mix.open_audio( mix::DEFAULT_FREQUENCY, mix::DEFAULT_FORMAT, 2, 1024)
	puts "Using audi driver:" + mix.driver_name
	music = mix::Music
	
	mus = music.load_audio("music_test.ogg");
	
	puts('Testing fade_in 3 times 2000 ms.')
	mus.fade_in(3, 2000);
	puts('ERROR: Music not fading in') unless mus.fading_in?
	
	puts('Waiting for fading_in? to end');
	while mus.fading_in? do Thread.pass end	

	puts('Testing volume')
	mus.volume = 60;
	puts('ERROR: Volume is not 60') if 60 != mus.volume
	
	puts('Testing pause')
	mus.pause
	puts('ERROR: Music not paused') unless mus.paused?

	puts('Sleeping 1 second');
    sleep 1

	puts('Testing resume')
	mus.resume
	puts('ERROR: Music not resumed') unless mus.playing?
	
	puts('Playing until end')
	helper = 0
	while mus.playing? do Thread.pass end
	# Test playing of music to the end
	puts('Played until end')
	puts('ERROR: Music not ended') if mus.playing?
	
	puts('Testing play 1 times')
	mus.play(1)
	puts('ERROR: Music not playing') unless mus.playing?
		
	puts('Testing fade_out')
	mus.fade_out(2000);
	puts('ERROR: Music not fading out ') unless mus.fading_out?
	
    puts('Fading until end')
    while mus.playing? or mus.fading_out? do Thread.pass end
	# Test playing of music to the end

	puts('ERROR: Music not ended') if mus.playing?
	mix.close_audio
end

music_thread = Thread.new do test_music() end
music_thread.join
Rubygame.quit()

